import sdk from "../core/index";
import { mathType } from "../core/tool/face";
import { Icard } from "../core/tool/Icard";
import { 盲盒枚举, 资源枚举, 道具枚举 } from "../shared/face/enums";
import bag_component from "./bag_component";
import { ItemType } from "../shared/face/enums";
import rpg_component from "./rpg_component";
import { AdminService } from "../service/AdminService";
import { TaskService } from "../service/TaskService";
import pos_component from "./pos_component";
import component, { ComType } from "../core/tool/component";

/**
 * 管理员组件 - 主要负责UI交互和状态管理
 * 职责：
 * 1. 管理系统状态展示
 * 2. 处理管理员操作
 * 3. 提供测试功能
 */
export default class admin_component extends component {
    constructor() {
        super();
    }

    init() {
        // 初始化管理员组件
    }
    /**
     * 管理员主界面
     */
    client_main() {
        const systemStatus = AdminService.getSystemStatus();

        const card = sdk.tool.card.create();
        card.title('🛠️ 系统管理');
        card.l(`在线人数: ${systemStatus.onlineCount}`);
        card.l(`bot连接: ${systemStatus.botConnections}`);
        card.l(`运行时间: ${Math.floor(systemStatus.serverUptime / 3600)}小时`);
        card.l(`内存使用: ${Math.round(systemStatus.memoryUsage.heapUsed / 1024 / 1024)}MB`);

        this.registerAdminButtonIntents(card);
        this.client_text(card.getText());
    }

    /**
     * 测试邮件发送
     */
    async test_mail() {
        const result = await AdminService.sendTestMail('1234567890');
        this.client_text(result.message);
    }

    /**
     * 测试改名卡
     */
    test_改名卡() {
        const testItem = AdminService.createTestItem(ItemType.道具, 道具枚举.强化碎片, 1);
        const bagCom = this.getComponent(ComType.背包) as bag_component;
        bagCom.addItem(testItem);
        this.client_text('✅ 改名卡已添加到背包');
    }

    /**
     * 强制下线
     */
    client_offline() {
        const result = AdminService.forceUserOffline(this.id);
        this.client_text(result.message);
    }

    /**
     * 系统存档和重启
     */
    async client_save() {
        const result = await AdminService.performSystemRestart(0);
        this.client_text(result.message);
    }
    /**
     * 测试升级
     */
    test_level_up() {
        const rpgCom = this.getComponent(ComType.rpg) as rpg_component;
        rpgCom.addExp(1000);
        this.client_text('✅ 获得1000经验值');
    }

    /**
     * 测试装备
     */
    test_prop() {
        const testItem = AdminService.createTestItem(ItemType.消耗品, "dev_技能书", 10);
        const bagCom = this.getComponent(ComType.背包) as bag_component;
        bagCom.addItem(testItem);

        const testItem1 = AdminService.createTestItem(ItemType.消耗品, "dev_血统", 999);
        bagCom.addItem(testItem1);

        const testItem2 = AdminService.createTestItem(ItemType.消耗品, "1", 2);
        bagCom.addItem(testItem2);

        this.client_text('✅ 测试道具已添加到背包');
    }

    test_equip() {
        const testItem = AdminService.createTestItem(ItemType.装备, "dev_装备", 1);
        const bagCom = this.getComponent(ComType.背包) as bag_component;
        bagCom.addItem(testItem);
        this.client_text('✅ 测试装备已添加到背包');
    }

    /**
     * 测试金币
     */
    test_gold() {
        const testItem = AdminService.createTestItem(ItemType.资源, 资源枚举.金币, 9999999);
        const bagCom = this.getComponent(ComType.背包) as bag_component;
        bagCom.addItem(testItem);
        this.client_text('✅ 大量金币已添加到背包');
    }
    // 注册管理员按钮意图的通用方法
    private registerAdminButtonIntents(card: Icard): void {
        const buttonConfigs = [
            { text: '存档', action: 'client_save', mathType: mathType.完全匹配 },
            { text: '下线', action: 'client_offline', mathType: mathType.完全匹配 },
            { text: '系统邮件测试', action: 'test_mail', mathType: mathType.完全匹配 },
            { text: '改名卡', action: 'test_改名卡', mathType: mathType.完全匹配 },
            { text: '升级', action: 'test_level_up', mathType: mathType.完全匹配 },
            { text: '测试装备', action: 'test_equip', mathType: mathType.完全匹配 },
            { text: '测试道具', action: 'test_prop', mathType: mathType.完全匹配 },
            { text: '测试金币', action: 'test_gold', mathType: mathType.完全匹配 },
            { text: '添加技能', action: 'test_skill', mathType: mathType.完全匹配 },
            { text: '测试永久提升', action: 'test_skill_permanent_boost', mathType: mathType.完全匹配 },
            { text: '升级技能', action: 'test_skill_upgrade', mathType: mathType.完全匹配 },
            { text: '重置全服任务', action: 'test_reset_server_tasks', mathType: mathType.完全匹配 },
            { text: '排行榜结算', action: 'test_ranking_settlement', mathType: mathType.完全匹配 },
            { text: '初始化排行榜', action: 'test_init_ranking', mathType: mathType.完全匹配 }
        ];

        this.registerButtonIntents(card, buttonConfigs);
    }

    test_skill_upgrade() {
        const rpgCom = this.getComponent(ComType.rpg) as rpg_component;
        rpgCom.selfSkill.forEach(skill => {
            rpgCom.upgradeSkill(skill.skillId);
        });
        this.client_text('✅ 技能已升级');
    }
    test_skill() {
        let id = 'FLAME_ENHANCEMENT';
        const rpgCom = this.getComponent(ComType.rpg) as rpg_component;
        rpgCom.addSk({ skillId: id, reName: '火焰强化', cooldown: 2, desc: '测试技能描述----' });
        id = 'BATTLE_INSTINCT';
        rpgCom.addSk({ skillId: id, reName: '战斗本能', cooldown: 2 });

        this.client_text('✅ 技能已添加到技能列表');
    }

    /**
     * 测试技能永久提升描述
     */
    test_skill_permanent_boost() {
        const rpgCom = this.getComponent(ComType.rpg) as rpg_component;
        
        // 添加火焰强化技能
        rpgCom.addSk({ skillId: 'FLAME_ENHANCEMENT', reName: '火焰强化', cooldown: 2 });
        
        // 模拟使用技能多次，获得永久提升
        const skillUseData = rpgCom.getSkillUseData('FLAME_ENHANCEMENT');
        if (skillUseData) {
            // 模拟使用5次后的永久提升
            skillUseData.permanentBoosts = { attRange: 0.5 };
            skillUseData.useCount += 5;
            
            // 重新加载技能
            rpgCom.reload_skills();
            
            this.client_text('✅ 火焰强化技能已添加，模拟使用5次后的永久提升');
        } else {
            this.client_text('❌ 技能添加失败');
        }

        // 添加战斗本能技能
        rpgCom.addSk({ skillId: 'BATTLE_INSTINCT', reName: '战斗本能', cooldown: 2 });
        
        // 模拟使用技能多次，获得永久提升
        const skillUseData2 = rpgCom.getSkillUseData('BATTLE_INSTINCT');
        if (skillUseData2) {
            // 模拟使用5次后的永久提升
            skillUseData2.permanentBoosts = { cooldown: 0.5 };
            skillUseData2.useCount += 5;
            
            // 重新加载技能
            rpgCom.reload_skills();
            
            this.client_text('✅ 战斗本能技能已添加，模拟使用5次后的永久提升');
        } else {
            this.client_text('❌ 技能添加失败');
        }

        // 添加多效果技能
        rpgCom.addSk({ skillId: 'MULTI_EFFECT_SKILL', reName: '多重效果技能', cooldown: 4 });
        
        // 模拟使用技能多次，获得永久提升
        const skillUseData3 = rpgCom.getSkillUseData('MULTI_EFFECT_SKILL');
        if (skillUseData3) {
            // 模拟使用3次后的永久提升
            skillUseData3.permanentBoosts = { attRange: 0.3 };
            skillUseData3.useCount += 3;
            
            // 重新加载技能
            rpgCom.reload_skills();
            
            this.client_text('✅ 多重效果技能已添加，模拟使用3次后的永久提升');
        } else {
            this.client_text('❌ 技能添加失败');
        }

        // 添加击杀强化技能
        rpgCom.addSk({ skillId: 'KILL_ENHANCEMENT', reName: '击杀强化', cooldown: 3 });
        
        // 模拟击杀敌人后的永久提升
        const skillUseData4 = rpgCom.getSkillUseData('KILL_ENHANCEMENT');
        if (skillUseData4) {
            // 模拟击杀3次敌人后的永久提升
            skillUseData4.permanentBoosts = { damage: 6 };
            skillUseData4.useCount += 3;
            
            // 重新加载技能
            rpgCom.reload_skills();
            
            this.client_text('✅ 击杀强化技能已添加，模拟击杀3次敌人后的永久提升');
        } else {
            this.client_text('❌ 技能添加失败');
        }

        // 添加暴击强化技能
        rpgCom.addSk({ skillId: 'CRITICAL_ENHANCEMENT', reName: '暴击强化', cooldown: 2 });
        
        // 模拟暴击后的永久提升
        const skillUseData5 = rpgCom.getSkillUseData('CRITICAL_ENHANCEMENT');
        if (skillUseData5) {
            // 模拟暴击5次后的永久提升
            skillUseData5.permanentBoosts = { criticalRate: 5 };
            skillUseData5.useCount += 5;
            
            // 重新加载技能
            rpgCom.reload_skills();
            
            this.client_text('✅ 暴击强化技能已添加，模拟暴击5次后的永久提升');
        } else {
            this.client_text('❌ 技能添加失败');
        }
    }

    /**
     * 测试重置全服任务
     */
    test_reset_server_tasks() {
        try {
            const result = TaskService.resetServerTasks();
            
            if (result.success) {
                this.client_text(`✅ ${result.message}\n📊 重置了 ${result.resetCount} 个全服任务`);
            } else {
                this.client_text(`❌ ${result.message}`);
            }
        } catch (error) {
            console.error('重置全服任务失败:', error);
            this.client_text('❌ 重置全服任务时发生错误');
        }
    }
    
    /**
     * 手动结算排行榜
     */
    async test_ranking_settlement() {
        try {
            await sdk.ranking.manualSettlement();
            this.client_text('✅ 排行榜结算完成');
        } catch (error) {
            console.error('排行榜结算失败:', error);
            this.client_text('❌ 排行榜结算失败');
        }
    }
    
    /**
     * 初始化排行榜数据
     */
    async test_init_ranking() {
        try {
            await sdk.ranking.initializeRankingData();
            this.client_text('✅ 排行榜数据初始化完成');
        } catch (error) {
            console.error('初始化排行榜数据失败:', error);
            this.client_text('❌ 初始化排行榜数据失败');
        }
    }
}

